package pms.qa.action;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import pms.qa.service.QaListService;
import pms.common.value.ProjectValue;
import pms.common.value.QaListValue;
import pms.common.value.QaSearchValue;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class QaListAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private QaListService qaListService;

	private QaSearchValue search;
	private QaListValue qalist;
	private Collection<QaListValue> qalists;
	private String[] banGos;
	private String all_select;
	private String quickSearchbuttonID;

	private int pageNow = 1 ; 
	private int pagePre = 0; 
	private int pageNext = 2; 
    private int pageSize = 10 ;
    private int totalPages;
    private int listsize=0;

	public String getQuickSearchbuttonID() {
		return quickSearchbuttonID;
	}
	public void setQuickSearchbuttonID(String quickSearchbuttonID) {
		this.quickSearchbuttonID = quickSearchbuttonID;
	}
	public QaListService getQaListService() {
		return qaListService;
	}
	public void setQaListService(QaListService qaListService) {
		this.qaListService = qaListService;
	}
	public QaListValue getQalist() {
		return qalist;
	}
	public void setQalist(QaListValue qalist) {
		this.qalist = qalist;
	}

	public QaSearchValue getSearch() {
		return search;
	}
	public void setSearch(QaSearchValue search) {
		this.search = search;
	}

	public Collection<QaListValue> getQalists() {
		return qalists;
	}
	public void setQalists(Collection<QaListValue> qalists) {
		this.qalists = qalists;
	}
	public String[] getBanGos() {
		return banGos;
	}
	public void setBanGos(String[] banGos) {
		this.banGos = banGos;
	}
	
	
	
	@SuppressWarnings("unchecked")
	public List getMonthArray() {
		List l_list = new ArrayList();
		for (int i=0;i<12;i++) {
			if (i<9) {
				l_list.add(i,"0" + (i+1));
			} else {
				l_list.add(i,"" + (i+1));
			}
		}
		return l_list;
	}
	
	@SuppressWarnings("unchecked")
	public List getDayArray() {
		List l_list = new ArrayList();
		for (int i=0;i<31;i++) {
			if (i<9) {
				l_list.add(i,"0" + (i+1));
			} else {
				l_list.add(i,"" + (i+1));
			}
		}
		return l_list;
	}

	//プロジェクト
	public List<ProjectValue> getProjectList() {
		return qaListService.getProjectList();
	}
	// 所属テーマ
	@SuppressWarnings("unchecked")
	public List<HashMap> getFunctionArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
        return qaListService.getFunctionArray();
    }

	// 提出者
	@SuppressWarnings("unchecked")
	public List<HashMap> getAskerArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
        return qaListService.getAskedArray();
    }

	// 問合先
	@SuppressWarnings("unchecked")
	public List<HashMap> getAskedArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
        return qaListService.getAskedArray();
    }
	
	// 状態
	@SuppressWarnings("unchecked")
	public List<HashMap> getStsArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
        return qaListService.getStsArray();
    }

	
	@SuppressWarnings("unchecked")
	public String execute() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		ArrayList qaliststmp = new ArrayList();
		qaliststmp = qaListService.getListData(search);
    	Map session = ActionContext.getContext().getSession();
    	session.put("qalists",qaliststmp);
    	setAll_select("");
    	pageCut();
		return SUCCESS;
	}

	//全件検索処理
	@SuppressWarnings("unchecked")
	public String allDataSearch() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		ArrayList qaliststmp = new ArrayList();
		search = new QaSearchValue();
		qaliststmp = qaListService.getListData(search);
    	Map session = ActionContext.getContext().getSession();
    	session.put("qalists",qaliststmp);
    	setAll_select("");
    	pageCut();
		return SUCCESS;
	}
	
	//速い条件検索処理
	public String quickSearch() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		System.out.println("quickSearchbuttonID --: "+quickSearchbuttonID);

		if("btn_1".equals(quickSearchbuttonID)){
			search.setProjectId("1");
		}
		else if("btn_2".equals(quickSearchbuttonID)){
			search.setProjectId("2");
		}
		else if("btn_3".equals(quickSearchbuttonID)){
			search.setProjectId("保険金SP");
		}
		else if("btn_4".equals(quickSearchbuttonID)){
			search.setProjectId("ＶＢ現行廃止");
		}
		else{
			return allDataSearch();
		}
		return execute();
	}
	
	@SuppressWarnings("unchecked")
	public String pageCut() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		qalists = new ArrayList<QaListValue>();
		banGosList();
		
		Map session = ActionContext.getContext().getSession();
		ArrayList alqalists = (ArrayList)session.get("qalists");
		
		setListsize(alqalists.size());
		setTotalPages(Integer.parseInt(String.valueOf(Math.ceil((listsize+0.0)/(pageSize+0.0))).substring(0,1)));
		
    	int min = (pageNow - 1) * pageSize;
    	int max = pageNow * pageSize;
    	if(listsize<min){
    		min = listsize;
    	}
    	if(listsize<max){
    		max = listsize;
    	}
    	List currentPageList = alqalists.subList(min,max);
    	for (int i=0;i<currentPageList.size();i++){
    		qalists.add((QaListValue)currentPageList.get(i));
    	}
    	return SUCCESS;
	}

	public void banGosList(){
		if (all_select == null || all_select.equals("")) {
			all_select = "";
		}
		if(all_select != ""){
			String banGostr = all_select;
			banGostr = banGostr.replace("[","").replace("]","");
			setBanGos(banGostr.split(","));
		}
		System.out.println("all_select : "+all_select);
	}
	//削除処理
	public String deleteData() {
		banGosList();
		boolean l_flg = qaListService.deleteData(banGos);
		if (l_flg) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	//email処理
	public String email() {
		banGosList();
		boolean l_flg = qaListService.sendMail(banGos);
		if (l_flg) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	//export
	public String getDownloadFileName() throws UnsupportedEncodingException {
		String downFileName = "qa_export_list.xls"; 
		downFileName = new String(downFileName.getBytes(), "ISO8859-1");
		return downFileName;
	}
	public InputStream getInputStream() throws Exception {
		banGosList();
		return qaListService.expExcelData(banGos);
	}
	public String export() throws Exception {
		return SUCCESS;
	}
	
	public int getPageNow() {
		return pageNow;
	}
	public void setPageNow(int pageNow) {
		setPagePre(pageNow-1);
		setPageNext(pageNow+1);
		this.pageNow = pageNow;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageNext() {
		return pageNext;
	}
	public void setPageNext(int pageNext) {
		this.pageNext = pageNext;
	}
	public int getPagePre() {
		return pagePre;
	}
	public void setPagePre(int pagePre) {
		this.pagePre = pagePre;
	}
	public int getTotalPages() {
		return totalPages;
	}
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	public int getListsize() {
		return listsize;
	}
	public void setListsize(int listsize) {
		this.listsize = listsize;
	}
	public String getAll_select() {
		return all_select;
	}
	public void setAll_select(String all_select) {
		this.all_select = all_select;
	}
	
}
